package com.example.administrator.zyfypt412lmy;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.zyfypt412lmy.fragment.Fragment5;
import com.example.administrator.zyfypt412lmy.fragment.Fragment6;

/**
 * Created by Cxc on 2017/5/11.
 */

public class FragmentFriend2 extends Fragment {
    private Fragment5 fragment3;
    private Fragment6 fragment4;
    /**
     * PagerSlidingTabStrip
     */
    private PagerSlidingTabStrip tabs;
    private DisplayMetrics dm;
    public FragmentFriend2() {

        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_friend2, null);
        initView(view);

        return view;
    }

    private void initView(View view) {

        dm = getResources().getDisplayMetrics();
        ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
        tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        pager.setAdapter(new FragmentFriend2.MyPagerAdapter(getChildFragmentManager()));
        tabs.setViewPager(pager);
        setTabsValue();

    }

    private void setTabsValue() {
        tabs.setShouldExpand(true);
        tabs.setDividerColor(Color.TRANSPARENT);
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));// 4
        tabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm)); // 16
        tabs.setIndicatorColor(Color.parseColor("#45c01a"));// #45c01a
        tabs.setSelectedTextColor(Color.parseColor("#45c01a"));// #45c01a
        tabs.setTabBackground(0);
    }


    public class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        private final String[] titles = {"案例", "项目"};

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:

                    if (null == fragment3) {
                        fragment3 = new Fragment5();
                    }

                    return fragment3;

                case 1:

                    if (null == fragment4) {
                        fragment4 = new Fragment6();
                    }

                    return fragment4;
                default:
                    return null;
            }
        }
    }
}
